package cn.hzw.supermarket.activity;

import static cn.hzw.supermarket.util.HttpUtil.sendHttpRequest;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.databinding.ActivityLoginBinding;
import cn.hzw.supermarket.domain.User;
import cn.hzw.supermarket.model.ResultInfo;
import okhttp3.OkHttpClient;

public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding binding;
    public static User user;
    public static User userOut = new User();
    final OkHttpClient client = new OkHttpClient();
    public static LoginActivity instance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏以及状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        user = new User();
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        instance = this;
        findViews();
    }

    private void findViews() {
        binding.ivDeleteUsername.setOnClickListener(v ->{
            binding.etUsername.setText("");
        });
        binding.ivDeletePassword.setOnClickListener(v ->{
            binding.etPassword.setText("");
        });
        binding.tvLogin.setOnClickListener(v ->{
            String user = binding.etUsername.getText().toString();
            String pass = binding.etPassword.getText().toString();
            if (user.isEmpty()){
                Toast.makeText(this, "请输入用户名！", Toast.LENGTH_SHORT).show();
            }else if (pass.isEmpty()){
                Toast.makeText(this, "请输入密码！", Toast.LENGTH_SHORT).show();
            }else {
                postRequest(user,pass);
                Log.d("TAG", "findViews: 11111111");
//                    Intent intent = new Intent(this,Loading3Activity.class);
//                    startActivity(intent);
            }
        });
        binding.tvRegister.setOnClickListener(v ->{
            Intent intent = new Intent(this,RegisterActivity.class);
            startActivityForResult(intent,1);
        });
        binding.layoutTitle.ivBack.setOnClickListener(v ->{
            finish();
        });
        binding.layoutTitle.tvTitle.setText("登录");
    }
    ResultInfo resultInfo;
    private void postRequest(String username,String password)  {
        new Thread(() -> {
            Map<String, String> map = new HashMap<>();
            map.put("username", username);
            map.put("password", password);
            String response = sendHttpRequest("/user/login", map);
            Log.d("TAG", "postRequest: "+response);
            if(response!=null&&!response.isEmpty()){
                resultInfo = JSONObject.parseObject(response, ResultInfo.class);//将JSON字符串转换成对象

                if (resultInfo.getMsg().equals("该账号已经登录！")){
                    runOnUiThread(() -> Toast.makeText(this, "当前账号已登录", Toast.LENGTH_SHORT).show());
                }
                else if(resultInfo.isSuccess()){
                    user=resultInfo.getUser();
                    userOut = resultInfo.getUser();
                    Log.d("TAG", "postRequestdfgh: "+userOut);
                    Intent intent=new Intent(LoginActivity.this,Loading3Activity.class);
                    startActivity(intent);
                } else
                    runOnUiThread(() -> Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_LONG).show());
            }
        }).start();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //如果请求码与返回码等于预期设置的值 则进行后续操作
        if (requestCode == 1 && resultCode == 2){
            //获取返回的数据
            String name = data.getStringExtra("data");
            String pass = data.getStringExtra("data2");
            //设置给页面的文本TextView显示
            binding.etUsername.setText(name);
            binding.etPassword.setText(pass);

        }
    }
}